using System.Linq;
using PhotoBlogApp.Core;
using PhotoBlogApp.Core.Models.Albums;
using PhotoBlogApp.Models.ClientModels;

namespace PhotoBlogApp.Models.Builders
{
    public class AlbumService : IAlbumService
    {
        private readonly IAlbumRepository albumRepository;

        public AlbumService(IAlbumRepository albumRepository)
        {
            this.albumRepository = albumRepository;
        }

        public AlbumM[] GetAlbums()
        {
            var albums = albumRepository
                    .GetAlbums(false)
                    .OrderByDescending(a => a.AlbumId)
                    .ToList()
                    .Select(BuildAlbumM)
                    .ToArray();
            return albums;
        }

        public AlbumM BuildAlbumM(Album album)
        {
            return new AlbumM
            {
                AlbumId = album.AlbumId,               
                Name = album.AlbumName,
                Logo = album.Logo,
                Description = album.Description,
                Photos = album.Photos.Select(p => new PhotoM
                {
                    PhotoId = p.PhotoId,
                    Url = string.Format(@"{0}\t\{1}", p.Path, p.PhotoName),
                    FullUrl = string.Format(@"{0}\{1}", p.Path, p.PhotoName),
                    Title = p.PhotoName,
                }).ToArray()
            };
        }
    }
}